home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Interactive Web Graphics with Shout 3D
/
Interactive Web Graphics With Shout 3D.iso
/
mac
/
Code
/
Chapter Code.exe
/
Chapter08
/
RotateMoveZoomPlusPanel.java
< prev
next >
Wrap
Text File
|
2000-09-08
|
3KB
|
140 lines
package applets;
import shout3d.*;
import shout3d.core.*;
import shout3d.math.*;
public class RotateMoveZoomPlusPanel extends Shout3DPanel implements DeviceObserver{
Viewpoint camera;
float fov;
int pixelStartX;
int pixelStartY;
int pixelEndX;
int pixelEndY;
float[] worldPos = new float[3];
float [] eulers = new float [3];
float [] axisAngle = new float [4];
Quaternion q = new Quaternion();
float rotationFactor = 30.0f; // pixels per radian
float translationFactor = .5f; //pixels per meter
float zoomFactor = 150.0f; //pixels per meter
public RotateMoveZoomPlusPanel (Shout3DApplet applet){
super(applet);
}
public void customInitialize() {
addDeviceObserver(this,"MouseInput", null);
camera = (Viewpoint) getCurrentBindableNode("Viewpoint");
worldPos = camera.position.getValue();
axisAngle = camera.orientation.getValue();
q.setAxisAngle(axisAngle);
q.getEulers(eulers);
fov = camera.fieldOfView.getValue();
String rotationFactorString = applet.getParameter("rotationFactor");
if (rotationFactorString != null){
rotationFactor = Float.valueOf(rotationFactorString).floatValue();
}
String translationFactorString = applet.getParameter("translationFactor");
if (translationFactorString != null){
translationFactor = Float.valueOf(translationFactorString).floatValue();
}
String zoomFactorString = applet.getParameter("zoomFactor");
if (zoomFactorString != null){
zoomFactor = Float.valueOf(zoomFactorString).floatValue();
}
}
protected void finalize() {
removeDeviceObserver(this,"MouseInput");
}
public boolean onDeviceInput(DeviceInput di, Object userData) {
MouseInput mi = (MouseInput) di;
switch (mi.which){
case MouseInput.DOWN:
pixelStartX = mi.x;
pixelStartY = mi.y;
return true;
case MouseInput.DRAG:
//if left button used
if (mi.button == 0) {
pixelEndX = mi.x;
pixelEndY = mi.y;
int dragDistanceX = pixelEndX - pixelStartX;
int dragDistanceY = pixelEndY - pixelStartY;
pixelStartX = pixelEndX;
pixelStartY = pixelEndY;
//ROTATION
float rotationDelta = -(dragDistanceX/rotationFactor);
eulers[0] = eulers[0] + rotationDelta;
q.setEulers(eulers);
q.getAxisAngle(axisAngle);
camera.orientation.setValue(axisAngle);
//TRANSLATION
float translationDelta = dragDistanceY/translationFactor;
float[] vector = {0,0, translationDelta};
q.xform(vector);
worldPos[0] = worldPos[0] + vector[0];
worldPos[2] = worldPos[2] + vector[2];
camera.position.setValue(worldPos);
return true;
}//end of 0 if
//if right button used
if (mi.button == 1) {
pixelEndY = mi.y;
int dragDistanceY = pixelEndY - pixelStartY;
float fovDelta = dragDistanceY/zoomFactor;
fov = fov + fovDelta;
camera.fieldOfView.setValue(fov);
pixelStartY = pixelEndY;
return true;
}//end of 1 if
}//end of switch
return false;
}
} //end of class